package com.story.aistory.config;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class Coze {
    public static final String spaceId = "7419142395348303922"; // Coze空间ID
    public static final String workflowId = "7480382079337070603"; // Coze工作流
    public static final String botId = "7419130443436048394"; // Coze机器人ID

    public static String createConversation() throws Exception {
        String CozeURL = "https://api.coze.cn/v1/conversation/create";
        HttpPost httpPost = new HttpPost(CozeURL);
        // 设置头字段
        httpPost.setHeader("Authorization", "Bearer " + JWTSignature.getToken());
        httpPost.addHeader("content-type", "application/json");
        // 发送请求
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(httpPost);
        // 处理返回结果
        String resCont = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
        System.out.println("\n################# 创建会话resCont #################"+resCont);
        JSONObject res = JSONObject.parseObject(resCont);
        String conversationId = res.getJSONObject("data").getString("id");

        return conversationId;
    }

    public static String createChat(String content, String conversationId) throws Exception {
        String CozeURL = "https://api.coze.cn/v3/chat" + "?conversation_id=" + conversationId;

        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(CozeURL);
        // 设置头字段
        httpPost.setHeader("Authorization", "Bearer " + JWTSignature.getToken());
        httpPost.addHeader("content-type", "application/json");

        // 设置内容信息
        JSONObject json = new JSONObject();
        json.put("bot_id", botId); // 机器人id
        json.put("user_id", "1234567"); // 用户id
        json.put("stream", false); // 流式传输
        json.put("auto_save_history", true); // 自动保存历史

        JSONObject additional_message = new JSONObject();
        additional_message.put("role", "user"); // 用户id
        additional_message.put("content", content); // 消息内容
        additional_message.put("content_type", "text"); // 消息类型
        json.put("additional_message", additional_message);

        StringEntity params = new StringEntity(json.toString());
        httpPost.setEntity(params);

        // 发送请求
        HttpResponse response = httpClient.execute(httpPost);

        // 处理返回结果
        String resCont = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
        JSONObject res = JSONObject.parseObject(resCont);
//        System.out.println("\n################# 创建消息resCont #################"+resCont);
        String chatId = res.getJSONObject("data").getString("id");

        return chatId;
    }

    public static String retrieveChat(String chatId, String conversationId) throws Exception {
        String CozeURL = "https://api.coze.cn/v3/chat/retrieve" + "?chat_id=" + chatId + "&conversation_id=" + conversationId;
        System.out.println(CozeURL);
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(CozeURL);
        // 设置头字段
        httpGet.setHeader("Authorization", "Bearer " + JWTSignature.getToken());
        httpGet.addHeader("Content-Type", "application/json");

//        // 发送请求
//        HttpResponse response = httpClient.execute(httpGet);
//
//        // 处理返回结果
//        String resCont = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
//        JSONObject res = JSONObject.parseObject(resCont);
//        System.out.println("\n################# 查看消息resCont #################"+resCont);
//        return resCont;
        return null;
    }

    public static JSONObject runWorkflow(String resume) throws Exception {
        String CozeURL = "https://api.coze.cn/v1/workflow/run"; // 非流式传输
        // String CozeURL = "https://api.coze.cn/v1/workflow/stream_run"; // 流式传输
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(CozeURL);
        // 设置头字段
        httpPost.setHeader("Authorization", "Bearer " + JWTSignature.getToken());
        httpPost.addHeader("content-type", "application/json; charset=UTF-8");
// 需要先对resume进行Base64编码
//        String encodedResume = Base64.getEncoder().encodeToString(resume.getBytes(StandardCharsets.UTF_8));
        // 设置内容信息
        JSONObject json = new JSONObject();
        json.put("workflow_id", workflowId); // 工作流id
        Map<String,Object> parameters = new HashMap<>();
        parameters.put("BOT_USER_INPUT", resume); // 输入
        json.put("parameters", parameters);
        StringEntity params = new StringEntity(json.toString(),"UTF-8");

        System.out.println(params);
        System.out.println(json.toString());
        httpPost.setEntity(params);
        // 发送请求
        HttpResponse response = httpClient.execute(httpPost);
        // 处理返回结果
        String resCont = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
        JSONObject res = JSONObject.parseObject(JSONObject.parseObject(resCont).getString("data"));
        System.out.println("\n################# 运行工作流resCont #################"+res);
        return res;
    }


    public static void deleteConversation() throws Exception {
        String CozeURL = "https://api.coze.cn/v1/conversation/message/delete";
        HttpPost httpPost = new HttpPost(CozeURL);
        // 设置头字段
        httpPost.setHeader("Authorization", "Bearer " + JWTSignature.getToken());
        httpPost.addHeader("content-type", "application/json");

        // 发送请求
        HttpClient httpclient = new DefaultHttpClient();
    }
}